import 'package:bot_toast/bot_toast.dart';
import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:video_player/video_player.dart';

class PlayerPage extends StatefulWidget {
  final String playUrl;

  const PlayerPage({Key key, this.playUrl}) : super(key: key);

  @override
  _PlayerPageState createState() => _PlayerPageState();
}

class _PlayerPageState extends State<PlayerPage> {
  VideoPlayerController _controller;
  ChewieController _chewieController;

  bool showController = true;

  @override
  void initState() {
    print('视频播放地址:${widget.playUrl}');

    SystemChrome.setPreferredOrientations(
        [DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight]);
    // 隐藏状态栏和底部按钮栏
    SystemChrome.setEnabledSystemUIOverlays([]);

    _controller = VideoPlayerController.network(widget.playUrl)
      ..initialize().then((value) => setState(() {}));

    _chewieController = ChewieController(
        videoPlayerController: _controller,
        autoPlay: true,
        looping: true,
        aspectRatio: 16 / 9,
        fullScreenByDefault: true,
        allowFullScreen: true,
        errorBuilder: (_, errorMessage) {
          BotToast.showText(text: errorMessage);
          Future.delayed(Duration(seconds: 2), () {
            Navigator.pop(context);
          });
          return Center(
            child: Icon(Icons.error),
          );
        });

    super.initState();
  }

  @override
  void dispose() {
    _controller?.dispose();
    _chewieController?.dispose();
    SystemChrome.setPreferredOrientations(
        [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
    // 隐藏底部按钮栏
    SystemChrome.setEnabledSystemUIOverlays(
        [SystemUiOverlay.top, SystemUiOverlay.bottom]);

// // 隐藏状态栏
//     SystemChrome.setEnabledSystemUIOverlays ([SystemUiOverlay.bottom]);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Chewie(controller: _chewieController),
    );
  }

// @override
// Widget build(BuildContext context) {
//   return Scaffold(
//     backgroundColor: Colors.black,
//     body: Stack(
//       children: [
//         GestureDetector(
//           onTapDown: (details) {
//             setState(() {
//               showController = !showController;
//             });
//           },
//           onTapUp: (details) {
//             Future.delayed(Duration(seconds: 3), (() {
//               setState(() {
//                 showController = false;
//               });
//             }));
//           },
//           child: Center(
//             child: _controller.value.isInitialized
//                 ? AspectRatio(
//                     aspectRatio: _controller.value.aspectRatio,
//                     child: VideoPlayer(_controller),
//                   )
//                 : Container(),
//           ),
//         ),
//         Positioned(
//             left: 0,
//             right: 0,
//             bottom: 0,
//             child: AnimatedOpacity(
//               duration: Duration(seconds: 3),
//               opacity: showController ? 1 : 0,
//               child: Visibility(
//                 visible: showController,
//                 child: Container(
//                   height: MediaQuery.of(context).size.height * 0.3,
//                   decoration: BoxDecoration(color: Colors.black38),
//                   child: Row(
//                     crossAxisAlignment: CrossAxisAlignment.center,
//                     mainAxisAlignment: MainAxisAlignment.center,
//                     children: [
//                       ElevatedButton(
//                         onPressed: () {
//                           setState(() {
//                             _controller.value.isPlaying
//                                 ? _controller.pause()
//                                 : _controller.play();
//                           });
//                         },
//                         child: Icon(
//                           _controller.value.isPlaying
//                               ? Icons.pause
//                               : Icons.play_arrow,
//                         ),
//                       ),
//                     ],
//                   ),
//                 ),
//               ),
//             ))
//       ],
//     ),
//   );
// }
}
